package com.yumao.animatordemo;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {

    int[] imageId = {R.id.one, R.id.two, R.id.three, R.id.four, R.id.five, R.id.six, R.id.seven};
    private List<ImageView> list;
    private boolean isShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        list = new ArrayList<>();
        for (int i = 0; i < imageId.length; i++) {
            ImageView im = (ImageView) findViewById(imageId[i]);
            im.setOnClickListener(this);
            list.add(im);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.one:
                if (isShow) {
                    closeAnimator();
                } else {
                    startAnimator();
                }
                isShow = !isShow;
                break;
            default:
                Toast.makeText(this, "" + view.getId(), Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void startAnimator() {
        for (int i = 1; i < imageId.length; i++) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(list.get(i), "translationY", 0f, 200 * i);
            animator.setDuration(500);
            animator.setStartDelay(500 * (i - 1));
            animator.setInterpolator(new BounceInterpolator());//弹动效果
            animator.start();
        }
    }

    private void closeAnimator() {
        for (int i = 1; i < imageId.length; i++) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(list.get(i), "translationY", 200 * i, 0f);
            animator.setDuration(500);
            animator.setStartDelay(500 * (i - 1));//动画延迟
            animator.start();
        }
    }
}
